using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class SlimeEnemy : Enemy
{
    void Start()
    {
        //anim = GetComponent<Animator>();
        //sprite = GetComponent<SpriteRenderer>();
        StartCoroutine(Attack());
    }
    void Update()
    {
        //Debug.Log(transform.right);
        transform.position += -transform.right * speed * Time.deltaTime;
    }

    private void FixedUpdate()
    {
        Vector2 startPos = transform.position - (transform.right * transform.localScale.x);
        Debug.DrawLine(startPos,startPos + Vector2.down, Color.red);
        RaycastHit2D hit1 = Physics2D.Raycast(startPos, Vector2.down, 1f, LayerMask.GetMask("Floor"));
        if (!hit1)
        {
            transform.right = -transform.right;
        }
    }

    private void OnTriggerEnter2D(Collider2D collision)
    {
        if (collision.gameObject.layer == LayerMask.NameToLayer("PlayerBullet"))
        {
            Debug.Log(HP);
            if (HP <= 0)
            {
                Destroy(gameObject);
            }
        }
    }

    IEnumerator Attack()
    {
        while (true)
        {
            Vector3 attackPos = GameObject.Find("Player").transform.position - this.transform.position;
            attackPos.z = 0f;
            attackPos = attackPos.normalized;
            Instantiate(bullet, transform.position, Quaternion.identity);
            yield return new WaitForSeconds(attckCd);
        }

    }
}
